<?php
	class ProjectManageAction extends Action {
		//方案首页
		public function project(){
			import('@.Action.Page');
			$term1=$this->_post('term1');				//获得赛区id
			$term2=$this->_post('term2');				//获得主题赛id
			$term3=$this->_post('term3');				//获得比赛状态id
			$term4=$this->_post('term4');				//获得学校或者团队
			$keyword=$this->_post('keyword');			//获得学校名字获得团队名字
			$where="";
			//当赛区和比赛状态不一起被选择时执行
			if(isset($term1)||isset($term2)||isset($term3)){
				if($term1 != 0){						//当赛区选择时执行
					$pvid=array();
					$province = M('province');			//实例化省份表
					$pvarr=$province->where('ZoneId = '.$term1)->field('Id')->select();		//获得省份id
					$pvn=count($pvarr);					//获得省份总数
					for($i=0;$i<$pvn;$i++){
						$pvid[]=$pvarr[$i]['Id'];		//组合成一维数组
					}
					//根据判断组合查询条件
					if(count($pvid)==1){			
						$hmap="ProvinceId =".$pvid[0];
					}elseif(count($pvid)==2){
						$hmap['ProvinceId']=array('in',$pvid);
					}else{
						$hmap['ProvinceId']=array('in',$pvid);
					}	
					$high=M('highschool');				//实例化学校表
					$hiarr=$high->where($hmap)->field('Id')->select();	//获取学校id
					$hin=count($hiarr);					//获取学校总数
					for($j=0;$j<$hin;$j++){
						$hiid[]=$hiarr[$j]['Id'];		//组合成一维数组
					}
					$temap['SchoolId']=array('in',$hiid);		//组合查询条件
					$team=M('team');					//实例化团队表
					$tearr=$team->where($temap)->field('Id')->select();		//获得团队id
					$ten=count($tearr);					//获得团队总数
					for($k=0;$k<$ten;$k++){
						$teid[]=$tearr[$k]['Id'];		//组合成一维数组
					}
					$testr=implode(',',$teid);			//组合成字符串
					$where='t.Id in ('.$testr.') and '; //组合查询条件
					$this->assign('term1',$term1);
				}
					
				if($term3 != 0){					//选择了比赛状态时执行
					$team=M('team');				//实例化团队表
					$teamarrid=$team->where('Status = '.$term3)->field('Id')->select();		//获取团队id
					$tnum=count($teamarrid);
					for($i=0;$i<$tnum;$i++){
						$tarrid[]=$teamarrid[$i]['Id'];			//组合成一维数组
					}
					$teamstr=implode(',',$tarrid);				//组合成字符串
					$where='t.Id in ('.$teamstr.') and ';		//组成查询条件
					$this->assign('term3',$term3);
				}
			
				if($term4 != 0){
					if($term4 == 1){			//如果选择团队执行
						$team=M('team');		//实例化团队名
						$tmarr=$team->where('Name like '."'%".$keyword."%'")->field('Id')->select();	//获取团队id
						$tmnum=count($tmarr);				//获取团队数量
						for($i=0;$i<$tmnum;$i++){
							$tmid[]=$tmarr[$i]['Id'];		//组合一维数组
						}
						$tmstr=implode(',',$tmid);			//组成字符串
						$where='t.Id in ('.$tmstr.') and ';	//组成查询条件
						$this->assign('term4',$term4);
						$this->assign('keyword',$keyword);	
					}
					
					if($term4 == 2){
						$team=M('team');					//实例化团队表
						$teaarr=$team->where('SchoolName like '."'%".$keyword."%'")->field('Id')->select();		//获得团队id
						$tean=count($teaarr);					//获得团队总数
						for($k=0;$k<$tean;$k++){
							$teaid[]=$teaarr[$k]['Id'];		//组合成一维数组
						}	
						$teastr=implode(',',$teaid);				//组合成字符串
						$where='t.Id in ('.$teastr.') and ';		//组成查询条件
						$this->assign('term4',$term4);
						$this->assign('keyword',$keyword);
					}
				}
			}
			
			if($trem1!=0 && $term3!=0 && $term4!=0){
				$province = M('province');			//实例化省份表
				$pvarr=$province->where('ZoneId = '.$term1)->field('Id')->select();		//获得省份id
				$pvn=count($pvarr);					//获得省份总数
				for($i=0;$i<$pvn;$i++){
					$pvid[]=$pvarr[$i]['Id'];		//组合成一维数组
				}
				//根据判断组合查询条件
				if(count($pvid)==1){			
					$hmap="ProvinceId =".$pvid[0];
				}elseif(count($pvid)==2){
					$hmap['ProvinceId']=array('in',$pvid);
				}else{
					$hmap['ProvinceId']=array('in',$pvid);
				}	
				$high=M('highschool');				//实例化学校表
				$hiarr=$high->where($hmap)->field('Id')->select();	//获取学校id
				$hin=count($hiarr);					//获取学校总数
				for($j=0;$j<$hin;$j++){
					$hiid[]=$hiarr[$j]['Id'];		//组合成一维数组
				}
				$temap['SchoolId']=array('in',$hiid);		//组合查询条件
				$team=M('team');					//实例化团队表
				$tearr=$team->where($temap)->field('Id')->select();		//获得团队id
				$ten=count($tearr);					//获得团队总数
				for($k=0;$k<$ten;$k++){
					$teid[]=$tearr[$k]['Id'];		//组合成一维数组
				}
				if(empty($teid)){
					$teid=array();				//如果没有结果声明成空数组
				}
			
				$team=M('team');				//实例化团队表
				$teamarrid=$team->where('Status = '.$term3)->field('Id')->select();		//获取团队id
				$tnum=count($teamarrid);
				for($i=0;$i<$tnum;$i++){
					$tarrid[]=$teamarrid[$i]['Id'];			//组合成一维数组
				}
				if(empty($tarrid)){
					$tarrid=array();				//如果没有结果声明成空数组
				}
				if($term4 == 1){			//如果选择团队执行
					$team=M('team');		//实例化团队名
					$tmarr=$team->where('Name like '."'%".$keyword."%'")->field('Id')->select();	//获取团队id
					$tmnum=count($tmarr);				//获取团队数量
					for($i=0;$i<$tmnum;$i++){
						$tmid[]=$tmarr[$i]['Id'];		//组合一维数组
					}
					if(empty($tmid)){
						$tmid=array();
					}	
				}
				if($term4 == 2){
					$team=M('team');					//实例化团队表
					$teaarr=$team->where('SchoolName like '."'%".$keyword."%'")->field('Id')->select();		//获得团队id
					$tean=count($teaarr);					//获得团队总数
					for($k=0;$k<$tean;$k++){
						$teaid[]=$teaarr[$k]['Id'];		//组合成一维数组
					}	
					if(empty($teaid)){
						$teaid=array();
					}
				}
				
				$allid=array_unique(array_merge($teid,$tarrid,$tmid,$teaid));	//合并数组并去掉重复的团队
				$allstr=implode(',',$allid);			//组成字符串
				$where='t.Id in ('.$allstr.') and ';	//组成查询条件
				$this->assign('term1',$term1);
				$this->assign('term3',$term3);
				$this->assign('term4',$term4);
				$this->assign('keyword',$keyword);
			}


			if($term2 != 0){					//选择了主题赛时执行
				$proth=M('projecttheme');		//实例化主题和方案联系表
				$tharr=$proth->where('ThemeId = '.$term2)->field('ProjectId')->select();	//获取方案id
				$thn=count($tharr);				//获取方案数量
				for($i=0;$i<$thn;$i++){					
					$thid[]=$tharr[$i]['ProjectId'];	//组成一维数组
				}
				$thstr=implode(',',$thid);				//组合字符串
				$where='p.Id in ('.$thstr.') and ';		//组合查询条件
				$this->assign('term2',$term2);
			}

			if($_POST['numPerPage']){
				$pagesize = $this->_post('numPerPage');
				$_GET['page'] = $this->_post('pageNum');//自定义翻页类需要 GET （当前页）信息
			}else{
				$pagesize=2;
			}	
			$com=M('competitionzone'); 	//实例化赛区表
			$clist=$com->field('Id,Name')->select();	//获取赛区
			$this->assign('clist',$clist);		//给模板赛区
			$theme=M('theme');			//实例化主题表
			$tlist=$theme->field('Id,Name')->select();	//获取比赛主题
			$this->assign('tlist',$tlist);		//发给模板
			$model=M();
			$totalCount=$model->table('project p,team t')->where($where.'p.TeamId=t.Id')->count();	//获取总数
			$this->assign('totalCount',$totalCount);				//把总数发给模板
			$page = new Page($totalCount,$pagesize);				//实例化分页类
			$pageinfo = $page->getPageInfo();						//获取分页
			$this->assign('currentPage',$pageinfo['page']);//分配当前页数
			$this->assign('pagesize',$pagesize);//分配一页显示的条数
			$arr=$model->table('project p,team t')->field('t.Id,p.Id,p.Name,p.Summarize,p.Poll,p.CommentCount,p.ViewCount,t.Logo,t.Name as tName,t.CaptainName,p.IsPublic')->where($where.'p.TeamId=t.Id')->limit($pageinfo['offset'],$pageinfo['num'])->select();			//获取方案和团队信息
		//	dump($arr);exit;
			$this->assign('arr',$arr);
			$this->display();
		}
		//是否公开
		public function hidden(){
			$id=$this->_get('pid');			//获取方案id
			$type=$this->_get('type');		//获取执行条件
			$pro=M('project');				//实例化方案表
			if($type==1){
				$arr['IsPublic']=0;
			}elseif($type==2){
				$arr['IsPublic']=1;
			}
			//更新方案公开状态
			if($pro->where('Id ='.$id)->save($arr)){
				$this->success_ajax('修改成功');
			}else{
				$this->error_ajax('修改失败');
			}
		}
		//删除方案
		function delete(){
			$id=$this->_get('pid');	//获取方案id
			$pro=M('project');		//实例化方案表
			//实例化相关表
			$proatta=M('projectattachfile');
			$procom=M('projectcomment');
			$procomm=M('projectcommentlimit');
			$propla=M(' projectplatform');
			$prorate=M('projectrates');
			$proth=M('projecttheme');
			$provo=M('projectvote');
			//删除相关联数据
			$res=$pro->where('Id='.$id)->delete();
			$proatta->where('ProjectId ='.$id)->delete();
			$procom->where('ProjectId ='.$id)->delete();
			$procomm->where('ProjectId ='.$id)->delete();
			$propla->where('ProjectId ='.$id)->delete();
			$proth->where('ProjectId ='.$id)->delete();
			$prorate->where('ProjectId ='.$id)->delete();
			$provo->where('ProjectId ='.$id)->delete();
			if($res){
				$this->success_ajax('删除成功');
			}else{
				$this->success_ajax('删除失败');
			}
		}	
	}
